## Temelia, C generic data structures library

# to build for debugging level 3, invoke make CFLAGS=-g3

## To build a DLL you have to type make

# compiler, archiver, linker
CC = $(CROSS_COMPILE)gcc
AR = $(CROSS_COMPILE)ar
LD = $(CROSS_COMPILE)ld
# configure dynamic linker
LDCONF = /sbin/ldconfig

# compiler flags. Optimization level etc.
CFLAGS_BASE=-O3 -Wall -g
CFLAGS += $(CFLAGS_BASE) -fpic

CROSS_FLAGS="-DBUILD_DLL -DDLL"

# project name
PRJNAME = temelia

# library name
LIBNAME = lib$(PRJNAME).so.1
# project's (svn) version
SVN_VER = $(shell svn info | cut -f2 -d" " | head -5 | tail -1)
# library real name
LIBREALNAME := $(LIBNAME).$(SVN_VER)
# library symbolic link name
LIBSYMNAME = lib$(PRJNAME).so

# repo directories
SRCDIR = src
HDRDIR = $(SRCDIR)/include
DEBDIR = deb
RPMDIR = rpm

# processor architectures
ARCH_32 = i386
ARCH_64 = amd64

# local install directories:
# local dir for headers
LOCINCDIR = usr/include/$(PRJNAME)
# local dir for library
LOCLIBDIR = usr/lib

# package names
PKGNAME_32 = $(PRJNAME)-1.0.$(SVN_VER)-1.$(ARCH_32)
PKGNAME_64 = $(PRJNAME)-1.0.$(SVN_VER)-1.$(ARCH_64)

# deb filesystem directory
DEBFS = filesystem

# deb files:
CTRLBKP_FILE = control.sav
DEBIAN_DIR = $(DEBFS)/DEBIAN
CTRL_FILE = $(DEBIAN_DIR)/control
POSTINST_FILE = $(DEBIAN_DIR)/postinst
PRERM_FILE = $(DEBIAN_DIR)/prerm

# variables:
# library install size
SIZE = `du $(DEBFS)/$(LOCINCDIR) | cut -f1`

HDRS = $(HDRDIR)/*.h
# list of object files. these should be C sources with their .c suffix changed to .o
# HACK: build all the C files in the directory
# should specify them by hand.
OBJS = $(shell ls $(SRCDIR)/*.c|tr '\n' ' '|sed -n -e s/\\.c/.o/gp)

# what to build: a shared library
all: $(LIBREALNAME)

# build the library, must have built the objects first
$(LIBREALNAME): $(OBJS)
	$(CC) -shared -Wl,-soname,$(LIBNAME) -o $(LIBREALNAME) $(OBJS) -lc

# given an object ($@) that has a corresponding C source ($<), build it
# add CROSS_FLAGS for a gcc cross-compiler
.c.o:
ifeq ($(CC), gcc)
	$(CC) $(CFLAGS) -c "$<" -o "$@"
else
	$(CC) $(CROSS_FLAGS) $(CFLAGS_BASE) -c "$<" -o "$@"
endif

# copy the header files and archive (needs the archive)
install: $(LIBREALNAME)
	-mkdir -p /$(LOCINCDIR)
	cp $(HDRS) /$(LOCINCDIR)
	-mkdir -p /$(LOCLIBDIR)
	cp $(LIBREALNAME) $(DESTDIR)/$(LOCLIBDIR)
	ln -s /$(LOCLIBDIR)/$(LIBREALNAME) /$(LOCLIBDIR)/$(LIBNAME)
	ln -s /$(LOCLIBDIR)/$(LIBREALNAME) /$(LOCLIBDIR)/$(LIBSYMNAME)
	$(LDCONF)

# build deb package (needs the archive)
deb32: $(LIBREALNAME) $(DEBDIR)/$(ARCH_32)/$(PKGNAME_32).deb 
deb64: $(LIBREALNAME) $(DEBDIR)/$(ARCH_64)/$(PKGNAME_64).deb

$(DEBDIR)/$(ARCH_32)/$(PKGNAME_32).deb: $(DEBDIR)/$(ARCH_32)
	-mkdir -p $</$(DEBFS)/$(LOCINCDIR)
	cp $(HDRS) $</$(DEBFS)/$(LOCINCDIR)
	-mkdir -p $</$(DEBFS)/$(LOCLIBDIR)
	cp $(LIBREALNAME) $</$(DEBFS)/$(LOCLIBDIR)
	-mkdir -p $</$(DEBIAN_DIR)
	echo "#!/bin/bash" >$</$(POSTINST_FILE)
	echo "ln -s /$(LOCLIBDIR)/$(LIBREALNAME) /$(LOCLIBDIR)/$(LIBNAME)" >>$</$(POSTINST_FILE)
	echo "ln -s /$(LOCLIBDIR)/$(LIBREALNAME) /$(LOCLIBDIR)/$(LIBSYMNAME)" >>$</$(POSTINST_FILE)
	echo "$(LDCONF)" >>$</$(POSTINST_FILE)
	echo "#!/bin/bash" >$</$(PRERM_FILE)
	echo "rm /$(LOCLIBDIR)/$(LIBSYMNAME)*" >>$</$(PRERM_FILE)
	echo "rm -r /$(LOCINCDIR)" >>$</$(PRERM_FILE)
	chmod +x $</$(POSTINST_FILE) $</$(PRERM_FILE)
	cp $</$(CTRLBKP_FILE) $</$(CTRL_FILE)
	echo "Installed-Size: $(SIZE)" >>$</$(CTRL_FILE)
	echo "Version: 1.0.$(SVN_VER)" >>$</$(CTRL_FILE)
	dpkg -b $</$(DEBFS) $</$(PKGNAME_32).deb

$(DEBDIR)/$(ARCH_64)/$(PKGNAME_64).deb: $(DEBDIR)/$(ARCH_64)
	-mkdir -p $</$(DEBFS)/$(LOCINCDIR)
	cp $(HDRS) $</$(DEBFS)/$(LOCINCDIR)
	-mkdir -p $</$(DEBFS)/$(LOCLIBDIR)
	cp $(LIBREALNAME) $</$(DEBFS)/$(LOCLIBDIR)
	-mkdir -p $</$(DEBIAN_DIR)
	echo "#!/bin/bash" >$</$(POSTINST_FILE)
	echo "ln -s /$(LOCLIBDIR)/$(LIBREALNAME) /$(LOCLIBDIR)/$(LIBNAME)" >>$</$(POSTINST_FILE)
	echo "ln -s /$(LOCLIBDIR)/$(LIBREALNAME) /$(LOCLIBDIR)/$(LIBSYMNAME)" >>$</$(POSTINST_FILE)
	echo "$(LDCONF)" >>$</$(POSTINST_FILE)
	echo "#!/bin/bash" >$</$(PRERM_FILE)
	echo "rm /$(LOCLIBDIR)/$(LIBSYMNAME)*" >>$</$(PRERM_FILE)
	echo "rm -r /$(LOCINCDIR)" >>$</$(PRERM_FILE)
	chmod +x $</$(POSTINST_FILE) $</$(PRERM_FILE)
	cp $</$(CTRLBKP_FILE) $</$(CTRL_FILE)
	echo "Installed-Size: $(SIZE)" >>$</$(CTRL_FILE)
	echo "Version: 1.0.$(SVN_VER)" >>$</$(CTRL_FILE)
	dpkg -b $</$(DEBFS) $</$(PKGNAME_64).deb

rpm32: deb32 $(RPMDIR)/$(ARCH_32)/$(PKGNAME_32).rpm 
rpm64: deb64 $(RPMDIR)/$(ARCH_64)/$(PKGNAME_64).rpm

$(RPMDIR)/$(ARCH_32)/$(PKGNAME_32).rpm: $(RPMDIR)/$(ARCH_32)
	-mkdir -p $<
	cd $<; alien -rck ../../$(DEBDIR)/$(ARCH_32)/$(PKGNAME_32).deb

$(RPMDIR)/$(ARCH_64)/$(PKGNAME_64).rpm: $(RPMDIR)/$(ARCH_64)

dll: $(OBJS)
	$(CC) -shared -o $(PRJNAME).dll -Wl,--out-implib,lib$(PRJNAME)dll.a $(OBJS)

dll_clean:
	-rm -rf $(PRJNAME).dll lib$(PRJNAME)dll.a
	-rm -f $(OBJS)

debclean:
	rm -rf $(DEBDIR)/$(ARCH_32)/$(DEBFS) $(DEBDIR)/$(ARCH_64)/$(DEBFS)
	rm -f $(DEBDIR)/$(ARCH_32)/*.deb $(DEBDIR)/$(ARCH_64)/*.deb

rpmclean:
	rm -rf $(RPMDIR)/$(ARCH_32)/*.rpm $(RPMDIR)/$(ARCH_64)/*.rpm
	
clean: debclean rpmclean
	rm -f $(OBJS) $(LIBSYMNAME)*

uninstall:
	-rm -r /$(LOCINCDIR)
	-rm /$(LOCLIBDIR)/$(LIBSYMNAME)*

.PHONY: clean debclean uninstall

